package pers.sports.controller;


import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import pers.sports.service.EmailService;
import pers.sports.service.ManagerService;
import pers.sports.service.StudentService;
import pers.sports.util.MD5Util;
import pers.sports.vo.LoginInfo;
import pers.sports.vo.StorageUser;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;


@Controller
@RequestMapping("/user")
public class UserController {

    private final StudentService studentService;
    private final ManagerService managerService;
    private final EmailService emailService;

    @Autowired
    public UserController(StudentService studentService, ManagerService managerService, EmailService emailService) {
        this.studentService = studentService;
        this.managerService = managerService;
        this.emailService = emailService;
    }

    /**
     * 判断用户身份以及密码是否正确
     *
     * @param loginInfo
     * @param request
     * @return 成功或失败
     * @throws UnsupportedEncodingException
     * @throws NoSuchAlgorithmException
     */
    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(@RequestBody LoginInfo loginInfo, HttpServletRequest request) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        Boolean p;
        int i = Integer.parseInt(loginInfo.getIdentity());
        StorageUser user = new StorageUser();
        if (i == 0) {
            p = studentService.StuPwdByNum(loginInfo);
            user.setIdentity("2");
        } else if (i == 1) {
            p = managerService.ManagerPwdByNum(loginInfo);
            user.setIdentity(managerService.findIdentityByName(loginInfo.getLoginName()));
        } else {
            p = false;
        }
        if (p) {
            user.setNum(loginInfo.getLoginName());
            request.getSession().setAttribute("user", user);
            return "success";
        } else {
            return "failure";
        }
    }

    //登出
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request) {
        request.getSession().invalidate();
        return "redirect:/";
    }

    /**
     * @param forget_info
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/forget", method = RequestMethod.POST)
    public String forget(@RequestBody Map<String, String> forget_info, HttpServletRequest request) {
        String loginName = forget_info.get("loginName");
        String email = forget_info.get("email");
        String code = forget_info.get("code");

        if (!studentService.StuEmailByNum(loginName, email)) {
            return "邮箱不匹配";
        } else {
            String new_code = request.getSession().getAttribute("email_code").toString();
            if (code.equals(new_code)) {
                studentService.updatePwdByNum(loginName, "4QrcOUm6Wau+VuBX8g+IPg==");
                return "success";
            } else {
                return "验证码不匹配";
            }
        }
    }

    @ResponseBody
    @RequestMapping(value = "/rePassword", method = RequestMethod.POST)
    public String rePassword(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        String old_password = jsonObject.getString("pwd");
        String new_password = jsonObject.getString("pwd_new1");
        StorageUser user = (StorageUser) request.getSession().getAttribute("user");
        String number = user.getNum();
        String identity = user.getIdentity();
        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setLoginName(number);
        loginInfo.setPwd(old_password);
        if ("2".equals(identity)) {
            Boolean b = studentService.StuPwdByNum(loginInfo);
            if (b) {
                studentService.updatePwdByNum(number, MD5Util.encoderPassword(new_password));
                request.getSession().invalidate();
                return "success";
            } else {
                return "您的旧密码输入错误";
            }
        } else {
            Boolean b = managerService.ManagerPwdByNum(loginInfo);
            if (b) {
                managerService.updatePwdByNum(number, MD5Util.encoderPassword(new_password));
                request.getSession().invalidate();
                return "success";
            } else {
                return "您的旧密码输入错误";
            }
        }
    }

    @ResponseBody
    @RequestMapping("/changeEmail")
    public String changeEmail(@RequestParam String email, @RequestParam String id) {
        Boolean p = studentService.updateEmailById(id, email);
        return p ? "修改成功" : "失败，请稍后再试。。";
    }

    /**
     * todo
     *  1.管理员公告管理  *
     *  2.管理部分功能隐藏
     *  3.公告新建页面显示  *
     *  4.权限管理
     *  5.学生申请志愿者*
     *  6.
     *  7.录入成绩*
     *  8.展示成绩*
     */


}

